<html>
	<head>
		<title> Project Barbalbero: Prova </title>
		<script type="text/javascript" src="jquery.js"></script>
		<!--<script type="text/javascript" src="Array.js"></script>
		<script type="text/javascript" src="XPath_client.js"></script>
		-->
		<script type="text/javascript" src="frameworkFunctions_client.js"></script>

		<script type="text/javascript">
		  function OnLoad () {
			//var ciao = __selectElements(document, ".//div[@id='ciao']")[0]; 
			//alert(ciao.outerHTML);
			//var path = ___getRelativePath(document, ciao);
			//alert(path);
			//alert(__selectElements(document, path)[0].outerHTML);
			
			
		    var hyper = __selectElements(document, ".//hypernode")[0];
			var serverNews = __selectElements(hyper, "./display")[0];
			var localNews = __selectElements(document, ".//div[@id='news']")[0];
			localNews.appendChild(serverNews);
			
			__registerEventListener(hyper, "./display", "DOMNodeInserted", function (event) {
				//news[0].appendChild(event.target);
				var localNews = __selectElements(document, ".//div[@id='news']")[0];
				//var updatedNews = __selectElements(hypers[0], ".//news/*");
				/*
				while (news.hasChildNodes()) {
					news.removeChild(news.firstChild);
				}
				//for (var n in updatedNews)	news.appendChild(updatedNews[n]);
				var serverNews = __selectElements(hypers[0], ".//news")[0];
				news.appendChild(serverNews);
				*/
				alert("remote event target="+event.target.outerHTML);
				/*if (localNews.hasChildNodes()) {
					localNews.insertBefore(event.target, localNews.firstChild);
				} else {
					localNews.appendChild(event.target);
				}*/
			});
			
			//var ultima = __selectElements(hypers[1], "./*");
			//for (n in serverNews)	news.appendChild(serverNews[n]);
			
		  }
		  
		  function AddNews() {
			var msg = __selectElements(document, ".//textarea[@name='message']")[0];
			
			var hyper = __selectElements(document, ".//hypernode")[0];
			
			var localNews = __selectElements(document, ".//div[@id='news']")[0];
			//var serverNews = __selectElements(hyper, "./display")[0];
			var serverNews = __selectElements(document, ".//div[@id='news']/display")[0];;
			
			var newElement = document.createElement("div");
			newElement.innerHTML = msg.value;
			
			//hypers[0].appendChild(serverNews);
			
			if (serverNews.hasChildNodes()) {
				serverNews.insertBefore(newElement, serverNews.firstChild);
			} else {
				serverNews.appendChild(newElement);
			}
			/*while (serverNews.hasChildNodes()) {
				serverNews.removeChild(serverNews.lastChild);
			}*/
			//serverNews.innerHTML += "<div>"+msg.value+"</div>";
			var nodoB = __selectElements(serverNews, "./b")[0];
			nodoB.innerHTML += msg.value;
			newElement.style.backgroundColor = "red";
			//serverNews.setAttribute("style", "background-color: yellow;");
			newElement.nextElementSibling.removeAttribute("style");
			
			//serverNews.innerHTML += "- extra";
			//newElement.innerHTML += "<div>extraindiv</div>";
			//newElement.innerHTML += " - extra";
			//newElement.appendChild(document.createElement("div"));
			
			//__remoteInsertBefore(hypers[1], "./div[1]", newElement);
			
			alert(serverNews.commit());
			
			
			//__setContents(hypers[0], "", __selectContents(hypers[0], "/news")[0]+"<div>"+msg[0]+"</div>");
			//__setContent(hypers[0], ".//news","<div>"+msg[0].value+"</div>");
			//__selectElements(hypers[0], ".//news/*");
			
		  }
		</script>
	</head>

	<body onload="OnLoad();">
		<div>
		  <p>Pagina di test</p>
		</div>
		<div id="news">
		</div>
		<div id="ciao"/>
		</div>
		<textarea name="message" cols="40" rows="5" value="Test message">Prova</textarea>
		<div>
		<button onclick="AddNews()">Aggiungi</button>
		</div>
		<hypernode refname="serverNews"></hypernode>
	</body>
</html>
	
